package leetcode.code2058;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.H;
import leetcode.helper.tree.ListNode;

public class Solution implements IDebug {

	public int[] nodesBetweenCriticalPoints(ListNode head) {
		ListNode R = head;
		if (R == null) {
			return new int[] { -1, -1 };
		}
		ListNode M = R;
		R = R.next;
		if (R == null) {
			return new int[] { -1, -1 };
		}
		ListNode L = M;
		M = R;
		R = R.next;
		List<Integer> poses = new ArrayList<>();
		int pos = 1;
		while (R != null) {
			if ((M.val > L.val && M.val > R.val) || (M.val < L.val && M.val < R.val)) {
				poses.add(pos);
			}
			pos++;
			L = M;
			M = R;
			R = R.next;
		}
		if (poses.size() < 2) {
			return new int[] { -1, -1 };
		}
		if (poses.size() == 2) {
			int ans = poses.get(1) - poses.get(0);
			return new int[] { ans, ans };
		}
		int last = poses.get(0);
		int min = Integer.MAX_VALUE;
		for (int i = 1; i < poses.size(); i++) {
			int cur = poses.get(i);
			if (cur - last < min) {
				min = cur - last;
			}
			last = cur;
		}
		return new int[] { min, poses.get(poses.size() - 1) - poses.get(0) };
	}

	@Override
	public void debug4() {
		this.nodesBetweenCriticalPoints(ListNode.set(H.str2array3("[1,3,2,2,3,2,2,2,7]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
